package contentSpider;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ContentCaptureWenDa implements ContentCapture {

	Log logger = LogFactory.getLog(ContentCaptureWenDa.class);

	// 抓取字符串中的有效内容部分
	public String getContent(String bodyString) {
		bodyString = bodyString.replaceAll("<a href=.*?help_ask_40.htm.*?来自手机问问.*?</a>", "").replaceAll("<strong>\\W*答案补充\\W*</strong>", "");
		final String mainHead = "<li class=\"answer\"><div class=\"q_content\"><div class=\"a_info\">参考答案</div>";
		final String reAskHead = "<div class=\"child_content author_reply\"><div class=\"c_a_info\">补充问题：</div>";
		final String replayHead = "<div class=\"child_content author_reply\"><div class=\"c_a_info\">补充问题：</div>";
		System.setProperty("LOG_DIR", "./");
		String mainContent = new String();
		Pattern titlePattern = Pattern.compile(titleRex);
		Matcher titleMatcher = titlePattern.matcher(bodyString);
		if (titleMatcher.find()) {
			title = titleMatcher.group(2).trim();
		}

		String finalContent = new String();
		for (int i = 0; i < 3;) {
			Pattern p = Pattern.compile(areaRex[i]);
			Matcher m = p.matcher(bodyString.replaceAll("<a href=\"http://cache.soso.com/wenwen/help/help_ask_40.htm\" target=\"_blank\"><span class=\"ico_mobile\">来自手机问问</span></a> ", ""));
			while (m.find()) {
				RandomContent rc = new RandomContent();
				Pattern justOnePattern = Pattern.compile(oneContentRex[i]);
				Matcher justOneMatcher = justOnePattern.matcher(m.group());
				while (justOneMatcher.find()) {
					Pattern mainPattern = Pattern.compile(mainContentRex[i]);
					Matcher mainMatcher = mainPattern.matcher(justOneMatcher.group());
					while (mainMatcher.find()) {
						mainContent = mainMatcher.group(2).replaceAll("<pre.*?>\\s*|</pre>\\s*", "").trim().replaceAll("\n", "<br/>").replaceAll(">\\s*", ">").replaceAll("\\s*<", "<");
					}
					Pattern additionalContentPattern = Pattern.compile(additionalContentRex[i]);
					Matcher additionalContentMatcher = additionalContentPattern.matcher(justOneMatcher.group());
					// System.out.println("111" +
					// additionalContentMatcher.find());
					while (additionalContentMatcher.find()) {
						String reask = "";
						if (i > 0) {
							// System.out.println("i = " + i);
							Pattern askPattern = Pattern.compile("问");
							Matcher askMatcher = askPattern.matcher(additionalContentMatcher.group(1));
							// System.out.println(additionalContentMatcher.group(1));
							if (askMatcher.find()) {
								reask = reAskHead;
							}
							Pattern answerPattern = Pattern.compile("答");
							Matcher answerMatcher = answerPattern.matcher(additionalContentMatcher.group(1));
							if (answerMatcher.find()) {
								reask = replayHead;
								// System.out.println(answerMatcher.group());
							}
						}
						String s = "</div>";
						if(i==0){
							s = "";
						}
						mainContent = mainContent + reask + additionalContentMatcher.group(2).replaceAll("<pre.*?>\\s*|</pre>\\s*", "").trim().replaceAll("\n", "<br/>") + s;

					}
					if(i>0){
						mainContent = mainHead + mainContent + "</div>" + "</li>";
					}
					
					rc.contentAdd(mainContent);

				}
				finalContent = finalContent + rc.getWycContent();
			}
			// System.out.println(i);
			i++;
		}
		return finalContent;
	}

	public void setContentRex(String rex) {

		Pattern p = Pattern.compile("(.*?),(.*?),(.*?),(.*)");
		Matcher m = p.matcher(rex);
		int i = 0;
		while (m.find()) {
			areaRex[i] = m.group(1);
			oneContentRex[i] = m.group(2);
			mainContentRex[i] = m.group(3);
			additionalContentRex[i] = m.group(4);
			// System.out.println(areaRex[i]);
			// System.out.println(oneContentRex[i]);
			// System.out.println(mainContentRex[i]);
			// System.out.println(additionalContentRex[i]);
			i++;
		}

	}

	public String getTitle() {
		return title;
	}

	String titleRex = null;
	String title = null;
	String delRex = null;

	public void setDelRex(String delRex) {
		this.delRex = delRex;
	}

	public void setTitleRex(String titleRex) {
		this.titleRex = titleRex;
	}

	String[] mainContentRex = new String[3];
	String additionalContentRex[] = new String[3];
	String areaRex[] = new String[3];
	String oneContentRex[] = new String[3];

}
